import time
import random
import logging
import datetime
import re
import redis
import pymysql

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from kuaipai_2 import KuaiPai

db_config = {
        'host':'127.0.0.1',
        'port':3306,
        'user': 'admin',
        'password': 'Root110qwe',
        'db': 'crm',
        'charset': 'utf8',
    }

def connect_redis_sql(db): #连接redis
    mdb = redis.StrictRedis(host='localhost', port=6379, db=db, decode_responses=True,password='ma1114')#
    return mdb

def connect_mysql_sql(cur,conn,sql_2,moshi='select'): #连接MySQL
    cur.execute(sql_2)
    conn.commit()
    if moshi == 'select':
        tuple_rcs = cur.fetchone()
        return tuple_rcs

def timer(func):
    def er_time(*args):
        now_start = datetime.datetime.now()
        func(*args)
        now_end = datetime.datetime.now()
        print(now_end-now_start)
    return er_time

@timer
def selenium_ceshi(*args):
    keyword,host_url, name_url = args
    res,search_input = a.run(keyword, host_url, name_url)
    search_input.clear()
    return res



logger = logging.getLogger('kuaipai_rank.log')

class KuaiPaiRank(KuaiPai):

    def remove_none(self,li):
        while True:
            try:
                a = li[-1]
                if not a:
                    li.pop(-1)
                else:
                    return li
            except:
                return li

    def open_baidu(self, keyword):
        # self.driver.maximize_window()
        self.driver.implicitly_wait(2)
        search_input=self.driver.find_element_by_xpath("//input[@id='kw']")
        search_input.send_keys(keyword)
        time.sleep(1)
        baidu = self.driver.find_element_by_xpath("//input[@id='su']")
        baidu.click()

        return search_input

    def checkout_host(self, host_url='', name_url=''):
        target_url_host = []
        res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]')
        if not res_divs:
            try:
                res_divs = WebDriverWait(self.driver, 5).until(
                    EC.presence_of_all_elements_located((By.XPATH, '//*[@id="content_left"]/div[@class="result c-container new-pmd"]')))
            except Exception as e:
                print(e)
                res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container new-pmd"]')
        else:
            try:
                res_divs = WebDriverWait(self.driver, 5).until(
                    EC.presence_of_all_elements_located((By.XPATH, '//*[@id="content_left"]/div[@class="result c-container "]')))
            except Exception as e:
                print(e)
                res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]')
        if res_divs:
            for i in res_divs:
                content = i.text
                target_url_host.append(content)
        content_left = '\n'.join(target_url_host)
        res = re.findall(r'{}'.format(host_url), content_left)
        if not res:
            res = re.findall(r'{}'.format(host_url.replace('www.', '')), content_left)
            if not res and name_url:
                res = re.findall(r'{}'.format(name_url), content_left)
        return self.remove_none(res)

    def click_next_page(self, func, host_url, name_url, page=0, number=0):
        # number 限制回调次数
        res = []
        self.biao = 0
        shu = number
        shu += 1
        host_url_s = host_url
        name_url_s = name_url
        for i in range(page, 10):
            try:
                res = func(host_url_s, name_url_s)
                if res:
                    return res
                next_page = self.driver.find_element_by_xpath('//*[@id="page"]/div/a[last()]')
                next_page.click()
            except Exception as e:
                if shu < 5:
                    logger.info(f'点击访问失败，将重新访问！{e}')
                    self.click_next_page(func, host_url_s, name_url_s, i, shu)
                    break
                else:
                    self.driver.quit()
                    raise Exception('ip有问题')
        return res

    def run(self,keyword,host_url,name_url):
        if random.randint(1, 101) <= 80:
            self.take_cookie()
        else:
            self.driver.get("http://www.baidu.com")
            self.driver.set_page_load_timeout(3)
        search_input = self.open_baidu(keyword)
        res = self.click_next_page(self.checkout_host,host_url,name_url)
        return res,search_input

if __name__=='__main__':
    sql_select = "SELECT id,keyword,host_url,name_url FROM tb_work WHERE is_delete=0;"
    while True:
        a = KuaiPaiRank('', '')
        a.take_cookie()
        conn = pymysql.connect(**db_config)
        cur = conn.cursor()
        mdb = connect_redis_sql(1)
        for i in range(2000):
            try:
                note = mdb.spop('kuaipai_rank')
                if note:
                    params = note.split('❤')
                    mid = params[0]
                    keyword = params[1]
                    host_url = params[2]
                    name_url = params[-1]
                    res = selenium_ceshi(keyword,host_url,name_url)
                    if not res:
                        sql_update = "UPDATE tb_work SET is_rank=0 WHERE id={}".format(int(mid))
                        connect_mysql_sql(cur,conn,sql_update,'update')
                        logger.info('id->{},keyword->{},host_url->{},name_url->{},rank->{}'.format(mid, keyword, host_url,name_url, '无排名'))
                    else:
                        sql_update = "UPDATE tb_work SET is_rank=1 WHERE id={}".format(int(mid))
                        connect_mysql_sql(cur, conn, sql_update, 'update')
                        logger.info('id->{},keyword->{},host_url->{},name_url->{},rank->{}'.format(mid, keyword, host_url,name_url, '有排名'))
                else:
                    time.sleep(60*60)
            except Exception as e:
                print(e)
                cur.close()
                conn.close()
                a.exit_chrome()
                time.sleep(60 * 60)
                break
            if i == 1999:
                cur.close()
                conn.close()
                a.exit_chrome()
                time.sleep(60 * 30)

